using System;
using Microsoft.SharePoint;
using System.Runtime.Remoting;
using System.Security.Policy;
using System.Reflection;
using System.Security.Principal;

using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using System.Collections;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web;
using System.Text;


namespace PhilWicklund.com.PollingWebPart.Utilities
{
    public class WSSUtilities : MarshalByRefObject, IWSSUtilities
    {
        public static void UdateListItemWithAppPoolIdentity(Guid siteID, Guid webID, string listName, int listItemID, string fieldName, string data)
        {
            RevertToAppPool reverter = new RevertToAppPool();
            reverter.UseAppPoolIdentity();
            using (RemoteInterfaceFactory remoteFactory = new RemoteInterfaceFactory())
            {

                // If the following call is not made, the Context is somewhat out of whack...
                WindowsIdentity.GetCurrent().Impersonate();

                remoteFactory.Loader.UpdateListItem(siteID, webID, listName, listItemID, fieldName, data);

            }
            reverter.ReturnToImpersonatingCurrentUser();
        }

        public void UpdateListItem(Guid siteID, Guid webID, string listName, int listItemID, string fieldName, string data)
        {
            try
            {
                using (SPSite site = new SPSite(siteID))
                {
                    using (SPWeb web = site.OpenWeb(webID))
                    {
                        SPList list = web.Lists[listName];
                        SPListItem item = list.GetItemById(listItemID);
                        SPField field = item.Fields.GetField(fieldName);

                        if (field.Type == SPFieldType.Boolean)
                            item[fieldName] = bool.Parse(data);
                        else if (field.Type == SPFieldType.Integer)
                            item[fieldName] = int.Parse(data);
                        else if (field.Type == SPFieldType.DateTime)
                            item[fieldName] = DateTime.Parse(data);
                        else if (field.Type == SPFieldType.Number)
                            item[fieldName] = double.Parse(data);
                        else
                            item[fieldName] = data;
                        item.Update();
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("ERROR: assembly could not update the listitem -> " + e.Message);
            }
        }

        public static bool ListExists(Guid siteID, Guid webID, string listName)
        {
            try
            {
                using (SPSite site = new SPSite(siteID))
                {
                    try
                    {
                        using (SPWeb web = site.OpenWeb(webID))
                        {
                            try
                            {
                                SPList list = web.Lists[listName];
                                return true;
                            }
                            catch (Exception)
                            {
                                return false;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("ERROR: could not load a web with that ID", ex);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("ERROR: could not load a site with that ID", e);
            }
        }

        public void Dispose() { }
    }
}